#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main() {
	int flag;
	char key[101] = {0};
	char s[101][101] = {0};
	char a[101][101] = {0};
	int n;
	scanf("%s", key);
	scanf("%d", &flag);
	scanf("%d", &n);
	for (int i  = 0; i < n; i++) {
		scanf("%s", s[i]);
	}
	if (flag == 0) {
		for (int i  = 0; i < n; i++) {
			strcpy(a[i], s[i]);
		}
		for (int i  = 0; i < n; i++) {
			for (int j = 0; s[i][j] != '\0'; j++) {
				s[i][j] = tolower(s[i][j]);
			}
		}
		for (int i = 0; key[i] != '\0'; i++) {
			key[i] = tolower(key[i]);
		}
		for (int i  = 0; i < n; i++) {
			if (strstr(s[i], key)) {
				printf("%s\n", a[i]);
			}
		}
	}
	if (flag == 1) {
		for (int i  = 0; i < n; i++) {
			if (strstr(s[i], key)) {
				printf("%s\n", s[i]);
			}
		}
	}
	return 0;
}